import 'package:enjoy_plus/pages/house/components/house_item.dart';
import 'package:enjoy_plus/utils/http.dart';
import 'package:flutter/material.dart';

class HouseList extends StatefulWidget {
  const HouseList({super.key});

  @override
  State<HouseList> createState() => _HouseListState();
}

class _HouseListState extends State<HouseList> {
  List houseList = [];

  @override
  void initState() {
    super.initState();
    // 获取列表
    _getList();
  }

  // 获取房屋列表
  _getList() async {
    // 13222223333 有房屋
    final res = await http.get('/room');
    setState(() {
      houseList = res['data'];
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color.fromARGB(255, 241, 238, 238),
      appBar: AppBar(
        // 显示隐藏方案2：Visibility 组件的 visible 控制显示或隐藏
        title: const Visibility(visible: true, child: Text('我的房屋')),
        backgroundColor: Colors.transparent,
        centerTitle: true,
      ),
      body: Stack(
        children: [
          Padding(
            padding: const EdgeInsets.only(bottom: 80, left: 10, right: 10),
            child: ListView.separated(
              itemBuilder: (BuildContext context, int index) {
                return HouseItem(
                  houseItem: houseList[index],
                );
              },
              separatorBuilder: (BuildContext context, int index) {
                return Container(
                  height: 10,
                  color: const Color.fromARGB(255, 241, 238, 238),
                );
              },
              itemCount: houseList.length,
            ),
          ),
          // 显示隐藏方案1： 通过三元表达式渲染组件
          houseList.length >= 6
              // 空组件
              ? const SizedBox()
              // 正常组件
              : Positioned(
                  bottom: 0,
                  left: 0,
                  right: 0,
                  child: Container(
                    padding: const EdgeInsets.all(10),
                    color: Colors.transparent,
                    child: Row(
                      children: [
                        Expanded(
                          child: ElevatedButton(
                            onPressed: () {
                              // 添加房屋(分步表单1. 获取地址)
                              Navigator.pushNamed(context, '/location_list');
                            },
                            child: const Column(
                              children: [
                                SizedBox(height: 5),
                                Icon(Icons.add),
                                Text('添加房屋'),
                                SizedBox(height: 5),
                              ],
                            ),
                          ),
                        )
                      ],
                    ),
                  ),
                )
        ],
      ),
    );
  }
}
